<template>
  <div>
    <scroller ref="my_scroller" :on-refresh="refresh" :on-infinite="infinite">
      <div
        style="margin:10px 0;background:#fff;padding:20px"
        v-for="(item, index) in detaillists1"
        :key="index"
      >
        <img
          @click="goiddetail(item)"
          :src="item.imagepath"
          alt
          style="width:80px;height:80px;float:left;margin-right:15px"
        />
        <span style="display:inline-block;width:70%;padding: 10px 0;" class="detail_span fs16">
          <span class="detail_span fs16" style="width:100%">
            {{item.itemname}}
            <span style="display:inline-block;float:right">{{item.num}}件</span>
          </span>
          <br />
          <span class="fs16 colc" style="padding-top:7px;display:inline-block;width:100%">
            ￥{{item.amount}}
            <span style="float:right">
              <button style="background:#ccc;color:#fff" @click="orderClert(item)">取消</button>
              <button style="background:rgb(0, 178, 163);color:#fff" @click="orderPay(item)">去支付</button>
            </span>
          </span>
        </span>
      </div>
    </scroller>
  </div>
</template>

<script>
export default {
  layout: 'default2',
  data() {
    return {
      ishowtk: false,
      ts: this.newStore(),
      ddList: [],
      orderno: '',
      des: '',
      tel: '',
      queryname: '',
      page: 1,
      pageSize: '10',
      detaillists1: [],
      detaillists2: [],
      prepayid: {}
    }
  },
  // watch: {
  //   queryname() {
  //     this.ddList = []
  //     const req = {
  //       token: localStorage.getItem('token'),
  //       query: this.queryname,
  //       page: '1',
  //       pageSize: '100000'
  //     }
  //     this.spost(this.ts, '/itemc/getusedlist', req).then(ts => {
  //       this.ddList = ts.d.data
  //     })
  //   }
  // },
  created() {
    // this.getddList()
    this.getorderlist()
    // this.getorderlist2()
  },
  methods: {
    //近详情
    goiddetail(item) {
      this.$router.push({
        name: 'forderdetail',
        query: {
          name: '我的订单',
          word: '订单详情',
          orderid: item.orderid
        }
      })
    },
    //取消订单
    orderClert(item) {
      var btnArray = ['取消', '确定']
      mui.confirm('是否确认取消订单?', '提示', btnArray, e => {
        if (e.index == 1) {
          const req = {
            token: localStorage.getItem('token'),
            orderno: item.orderno
          }
          this.spost(this.ts, '/orderc/orderClert', req).then(ts => {
            if (ts.d.flag == '200') {
              this.getorderlist()
              mui.toast('取消成功')
            }
          })
        }
      })
    },
    //支付
    orderPay(item) {
      const req = {
        token: localStorage.getItem('token'),
        orderno: item.orderno
      }
      this.spost(this.ts, '/orderc/orderPay', req).then(ts => {
        if (ts.d.flag == '200') {
          this.prepayid = ts.d.data
          this.PayMoney()
        }
      })
    },
    PayMoney() {
      if (typeof WeixinJSBridge == 'undefined') {
        if (document.addEventListener) {
          document.addEventListener(
            'WeixinJSBridgeReady',
            this.onBridgeReady,
            false
          )
        } else if (document.attachEvent) {
          document.attachEvent('WeixinJSBridgeReady', this.onBridgeReady)
          document.attachEvent('onWeixinJSBridgeReady', this.onBridgeReady)
        }
      } else {
        this.onBridgeReady()
      }
    },
    onBridgeReady() {
      WeixinJSBridge.invoke(
        'getBrandWCPayRequest',
        {
          timeStamp: `${this.prepayid.timeStamp}`, // 时间戳，自1970年以来的秒数
          appId: `${this.prepayid.appId}`, // 公众号名称，由商户传入
          nonceStr: `${this.prepayid.nonceStr}`, // 随机串
          package: `${this.prepayid.package}`, //
          signType: `${this.prepayid.signType}`, // 微信签名方式
          paySign: `${this.prepayid.paySign}` // 微信签名
        },
        res => {
          // alert(JSON.stringify(res))
          // alert(`输入密码后从微信服务器返回来的支付结果：${JSON.stringify(res)}`);
          if (res.err_msg == 'get_brand_wcpay_request:ok') {
            console.log('res.err_msg', res.err_msg)
            this.getorderlist()
            this.getorderlist2()
            // this.$router.push({ name: 'eporder' })
            // this.$router.push({
            //   name: 'chufang-payOff',
            //   query: {
            //     je_sf: this.je_sf,
            //     payResult: true,
            //     payMessage: '支付成功'
            //   }
            // })
          } else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
            // this.$router.push({ name: 'payOff', query: { je_sf: this.je_sf, payResult: false, payMessage: '取消支付' } });
            // this.showAlert = true
            // this.alertText = '已取消支付'
            console.log('res.err_msg', res.err_msg)
          } else {
            // this.$router.push({ name: 'payOff', query: { je_sf: this.je_sf, payResult: false, payMessage: res.err_msg } });
            // this.showAlert = true
            // this.alertText = `支付失败,错误信息${res.err_msg}`
            console.log('res.err_msg', res.err_msg)
          }
        }
      )
    },
    //搜索
    dosearch() {
      this.page = 1
      this.getorderlist()
      this.getorderlist2()
    },
    //调用列表1
    getorderlist(fn) {
      const req = {
        token: localStorage.getItem('token'),
        paystatus: '0',
        page: this.page + '',
        pageSize: this.pageSize
      }
      this.spost(this.ts, '/orderc/getorderlist', req).then(ts => {
        if (ts.d.data.length < 1) {
          fn(true)
          return
        } else if (fn) {
          fn()
        }
        if (this.page == 1) {
          this.detaillists1 = []
          this.detaillists1 = ts.d.data
        } else {
          this.detaillists1 = this.detaillists1.concat(ts.d.data)
        }
        console.log(this.detaillists1)

        // this.detaillists1 = ts.d.data
      })
    },
    //调用列表2
    getorderlist2() {
      const req = {
        token: localStorage.getItem('token'),
        paystatus: '1',
        page: this.page + '',
        pageSize: this.pageSize
      }
      this.spost(this.ts, '/orderc/getorderlist', req).then(ts => {
        this.detaillists2 = ts.d.data
      })
    },
    dotuikuan() {
      if (this.tel == '') {
        mui.toast('请输入退款手机号', { duration: '1000', type: 'div' })
        return
      }
      if (this.des == '') {
        mui.toast('请输入退款原因', { duration: '1000', type: 'div' })
        return
      }
      const req = {
        token: localStorage.getItem('token'),
        orderno: this.orderno,
        des: this.des,
        tel: this.tel
      }
      this.spost(this.ts, '/Orderc/addRefund', req).then(ts => {
        if (ts.d.flag == '200') {
          mui.toast('申请提交成功', { duration: '1000', type: 'div' })
          this.ishowtk = false
          this.dosearch()
        } else {
          mui.toast(ts.d.message, { duration: '1000', type: 'div' })
        }
      })
    },
    refresh(done) {
      this.page = 1
      this.getorderlist(done)
      setTimeout(() => {
        this.$refs.my_scroller.finishPullToRefresh()
      }, 200)
    },
    infinite(done) {
      setTimeout(() => {
        this.page += 1
        this.getorderlist(done)
      }, 500)
    },
    // goddpage3(item) {
    //   this.$router.push({
    //     name: 'ddpage2',
    //     query: {
    //       from: '订单',
    //       word: '订单详情',
    //       orderno: item.orderno
    //     }
    //   })
    // },
  }
}
</script>

<style scoped>
.popdiv {
  position: fixed;
  z-index: 80;
  width: 80%;
  left: 10%;
  top: 20%;
  background: #f1f1f1;
  padding: 20px;
}
.mui-backdrop {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 998;
  background-color: rgba(0, 0, 0, 0.3);
}
.moneyinfo {
  text-align: right;
  display: inline-block;
  width: 100%;
  color: rgb(143, 143, 148);
  line-height: 20px;
}
.maincontent {
  padding: 15px;
  width: 96%;
  margin: 10px auto;
  border-radius: 5px;
}
.ddcontent {
  display: flex;
  justify-content: space-between;
}
.ddcontent:first-child {
  margin-top: 0;
}
.ddimg {
  width: 30%;
}
.ddinfo {
  width: 50%;
  padding: 0 10px;
}
.ddinfo p:first-child {
  color: #333;
  line-height: 18px;
}
.money {
  padding: 15px 0;
  padding-bottom: 6px;
  text-align: right;
  color: #333;
}
.btn {
  height: 15px;
}
.btn span {
  padding: 5px 10px;
  border-radius: 15px;
  border: 1px solid #ccc;
  margin: 0 5px;
  float: right;
  letter-spacing: 2px;
}
.detail_span {
  display: inline-block;
  line-height: 25px;
}
</style>
